package com.olc.railing.http;

import android.content.Context;

import com.google.gson.Gson;
import com.olc.railing.constant.Constants;
import com.olc.railing.constant.Urls;
import com.olc.railing.model.GasOrderInfo;
import com.olc.railing.utils.MyLog;
import com.tyxh.paylibrary.exception.NetNotAvailableException;
import com.tyxh.paylibrary.exception.NullException;
import com.tyxh.paylibrary.listener.ResultCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.etcext.commons.tools.sm2.SM2Utils;
import io.etcext.config.SM2Config;

public class WrapperNetBusiness {
    /**
     * 获取未支付订单
     * @param data    "data": "{ "plateNumber": "京A12345", "gunNumber": "1"}"
     * @throws JSONException
     */

    public static void fetchNoPayOrder(Context context , String data,GasCallBack<GasOrderInfo> gasCallBack) throws JSONException, IOException, NullException, NetNotAvailableException {
        MyLog.e(MyLog.TAG,"encryptData "+ data);
        String encryptData = SM2Config.doEncryption(data,Constants.PUBLIC_KEY);
        HttpBusiness.getInstance().post(context, Urls.getOilUrl(context), encryptData, new ResultCallBack<CommonBackJson>() {
            @Override
            public void onFail(int i, Exception e) {
                gasCallBack.onError("104");
            }

            @Override
            public void onResponse(int i, CommonBackJson commonBackJson) {
                try {
                    if("100".equals(commonBackJson.getCode())){
                        GasOrderInfo gasOrderInfo =  new Gson().fromJson(new JSONArray(commonBackJson.getData()).getJSONObject(0).toString(), GasOrderInfo.class);
                        gasCallBack.onResponse(gasOrderInfo);
                    }else{
                        gasCallBack.onError(commonBackJson.getCode());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
         /*       GasOrderInfo gasOrderInfo =  new Gson().fromJson("{\"payMoney\":21024,\"amount\":1,\"totalMoney\":21024,\"gunId\":\"577001054bbf4724af99cfb0fedd8384\",\"litter\":\"40.43\",\"unit\":\"L\",\"otherOrderId\":\"20211102115627y892s8h04429777485\",\"unitPrice\":520,\"sellingPrice\":21024,\"goodsType\":1, \"goodsCode\":\"92\"}", GasOrderInfo.class);
                gasCallBack.onResponse(gasOrderInfo);*/

            }

            @Override
            public void onHttpFail(int i, String s) {
                gasCallBack.onError("104");
            }
        });

     }


    /**
     * 获取未支付订单
     * @param data
     * @throws JSONException
     */

    public static void synPayedOrder(Context context ,String data,GasCallBack<String> gasCallBack) throws  NullException, NetNotAvailableException {
        String encryptData = SM2Config.doEncryption(data,Constants.PUBLIC_KEY);
        HttpBusiness.getInstance().post(context, Urls.getOilUrl(context), encryptData, new ResultCallBack<CommonBackJson>() {
            @Override
            public void onFail(int i, Exception e) {
                gasCallBack.onError("104");
            }

            @Override
            public void onResponse(int i, CommonBackJson commonBackJson) {

                if("100".equals(commonBackJson.getCode())){
                    gasCallBack.onResponse(commonBackJson.getData());
                }else{
                    gasCallBack.onError(commonBackJson.getCode());
                }
            }

            @Override
            public void onHttpFail(int i, String s) {
                gasCallBack.onError("104");
            }
        });

    }

}
